package com.example.liuxing.jdmall.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.liuxing.jdmall.MainActivity;
import com.example.liuxing.jdmall.R;
import com.example.liuxing.jdmall.listener.IBottomBarClickListener;

/**
 * Created by liuxing on 2017/7/17.
 */

public class Bottom_bar extends LinearLayout implements View.OnClickListener {
    private ImageView homeIv;
    private TextView homeTv;

    private ImageView categoryIv;
    private TextView categoryTv;

    private ImageView shopcarIv;
    private TextView shopcarTv;

    private ImageView mineIv;
    private TextView mineTv;

    private IBottomBarClickListener iBottomBarClickLinster; // 接口

    private int mCurrentTableId; // 保存当前点击的ID

    public Bottom_bar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public void setBottomBarClickListener(IBottomBarClickListener listenner) {
        iBottomBarClickLinster = listenner;
    }

    @Override
    protected void onFinishInflate() { // 当所有的控件测量排布完成后
        super.onFinishInflate();
        findViewById(R.id.frag_main_liner).setOnClickListener(this);
        findViewById(R.id.frag_category_liner).setOnClickListener(this);
        findViewById(R.id.frag_shopcar_liner).setOnClickListener(this);
        findViewById(R.id.frag_mine_liner).setOnClickListener(this);

        homeIv = (ImageView)findViewById(R.id.frag_main_iv);
        homeTv = (TextView)findViewById(R.id.frag_main_tv);

        categoryIv = (ImageView)findViewById(R.id.frag_category_iv);
        categoryTv = (TextView)findViewById(R.id.frag_category_tv);

        shopcarIv = (ImageView)findViewById(R.id.frag_shopcar_iv);
        shopcarTv = (TextView)findViewById(R.id.frag_shopcar_tv);

        mineIv = (ImageView)findViewById(R.id.frag_mine_iv);
        mineTv = (TextView)findViewById(R.id.frag_mine_tv);

        // 默认点击首页
        findViewById(R.id.frag_main_liner).performClick();
    }

    @Override
    public void onClick(View v) {

        if (mCurrentTableId == v.getId()) {
            return;
        }

        homeIv.setImageDrawable(getResources().getDrawable(R.drawable.bot_bar_home));
        categoryIv.setImageDrawable(getResources().getDrawable(R.drawable.bot_bar_category));
        shopcarIv.setImageDrawable(getResources().getDrawable(R.drawable.bot_bar_shopcar));
        mineIv.setImageDrawable(getResources().getDrawable(R.drawable.bot_bar_mime));

        homeTv.setSelected(false);
        categoryTv.setSelected(false);
        shopcarTv.setSelected(false);
        mineTv.setSelected(false);

        switch (v.getId()) {
            case R.id.frag_main_liner :
                homeIv.setImageDrawable(getResources().getDrawable(R.drawable.bot_bar_home_selected));
                homeTv.setSelected(true);
                break;
            case R.id.frag_category_liner :
                categoryIv.setImageDrawable(getResources().getDrawable(R.drawable.bot_bar_category_selected));
                categoryTv.setSelected(true);
                break;
            case R.id.frag_shopcar_liner :
                shopcarIv.setImageDrawable(getResources().getDrawable(R.drawable.bot_bar_shopcar_selected));
                shopcarTv.setSelected(true);
                break;
            case R.id.frag_mine_liner :
                mineIv.setImageDrawable(getResources().getDrawable(R.drawable.bot_bar_mime_selected));
                mineTv.setSelected(true);
                break;
        }

        if (iBottomBarClickLinster != null) {
            iBottomBarClickLinster.onItemClick(v.getId());
            mCurrentTableId = v.getId();
        }
    }

}
